﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GrouponDesktop
{
    public partial class RegistroUsuario : Form
    {
        private DataGridView dgv_ciudades = new DataGridView();
       
        public RegistroUsuario()
        {
            InitializeComponent();
            p_cliente.Hide();
            p_provee.Hide();
            
            BDConnection.FillComboBox(cb_local, "select ciudad from CodingSpree.TL_CIUDADES");
            BDConnection.FillComboBox(cb_ciudad, "select ciudad from CodingSpree.TL_CIUDADES");
            BDConnection.FillComboBox(cb_rubro, "select rubro from CodingSpree.TL_RUBROS");
            

            DataGridViewColumn ciudades = new DataGridViewColumn();
            DataGridViewCell cell = new DataGridViewTextBoxCell();
            ciudades.CellTemplate = cell;
            ciudades.Name = "ciudades";
            ciudades.ValueType = typeof(string);
            dgv_ciudades.Columns.Add(ciudades);
            tb_fechaNac.Format = DateTimePickerFormat.Custom;
            tb_fechaNac.CustomFormat = "yyyy-MM-dd";
            //dgv_ciudades.Show();
            //dgv_ciudades.Width = 600;
            //dgv_ciudades.Height = 600;
            //dgv_ciudades.Visible = true;
            //dgv_ciudades.Location = new Point(15, 15);
            //dgv_ciudades.ReadOnly = true;
            //dgv_ciudades.AllowUserToAddRows = false;
            //this.Controls.Add(dgv_ciudades);
        
        
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb_tipo.Text == "Cliente") //SI EL TIPO ES CLIENTE MUESTRA LA FORMA DE CLIENTE
            {
                p_cliente.Show();
                p_provee.Hide();
                cb_rol.Items.Clear();
                BDConnection.FillComboBox(cb_rol, "EXEC CodingSpree.listarRoles '" + 1 + "'");
            }

            if (cb_tipo.Text == "Proveedor") //SI ES PROVEEDOR MUESTRA LA FORMA DE PROVEEDOR
            {
                p_cliente.Hide();
                p_provee.Show();
                cb_rol.Items.Clear();
                BDConnection.FillComboBox(cb_rol, "EXEC CodingSpree.listarRoles '" + 2 + "'");
            }

        }

        private void b_select_Click(object sender, EventArgs e)
        {
            BuscarReg br = new BuscarReg(this.dgv_ciudades, this.tb_ciudades);
            br.Show();
        }

        private void b_alta_Click(object sender, EventArgs e)
        {

            string query = "EXEC CodingSpree.existeusuario '" + this.tb_user.Text + "'";
            string result = BDConnection.QueryAndExecuteScalarString(query);


            if (result == "1")
            {

                if (tb_pass1.Text == tb_pass2.Text && tb_pass1.Text != "") // LAS PASSWORDS NO COINCIDEN
                {

                    if (tb_apellido.Text != "" && tb_nombre.Text != "" && tb_dni.Text != "" && tb_mail.Text != "" && tb_telefono.Text != "" && tb_dir.Text != "" && cb_local.Text != "")
                    {// NO INGRESO TODOS LOS DATOS
                               
                        query = "EXEC CodingSpree.existeTelefono '" + tb_telefono.Text  + "'";
                        result = BDConnection.QueryAndExecuteScalarString(query);

                        if (result == "1")
                        {

                            string password = Funciones.ImprimirHash(tb_pass1.Text);
                            int rol = BDConnection.QueryAndExecuteScalar("select id_roles from CodingSpree.TL_ROLES where desc_rol = '" + cb_rol.Text + "'");   

                            BDConnection.execute("EXEC CodingSpree.insertUser '" + this.tb_user.Text + "', '" + password + "', " + rol);

                            query = "EXEC CodingSpree.insertCliente '";
                            query = query + this.tb_nombre.Text + "','";
                            query = query + this.tb_apellido.Text + "', ";
                            query = query + this.tb_dni.Text + ",'";
                            query = query + this.tb_mail.Text + "', ";
                            query = query + this.tb_telefono.Text + ",'";
                            query = query + this.tb_fechaNac.Text + "','";
                            query = query + this.tb_dir.Text + "', ";
                            query = query + this.tb_piso.Text + ",'";
                            query = query + this.tb_dept.Text + "', ";
                            query = query + this.tb_cp.Text + ",'";
                            query = query + this.cb_local.Text + "'";
                            BDConnection.execute(query);

                            for (int i = 0; i < (this.dgv_ciudades.Rows.Count - 1); i++)
                            {
                                password = Funciones.ImprimirHash(tb_pass1.Text);
                                BDConnection.execute("EXEC CodingSpree.insertUser '" + this.tb_user.Text + "', '" + password + "', " + 1);

                                query = "EXEC CodingSpree.insertCliente '";
                                query = query + this.tb_nombre.Text + "','";
                                query = query + this.tb_apellido.Text + "', ";
                                query = query + this.tb_dni.Text + ",'";
                                query = query + this.tb_mail.Text + "', ";
                                query = query + this.tb_telefono.Text + ",'";
                                query = query + this.tb_fechaNac.Text + "','";
                                query = query + this.tb_dir.Text + "', ";
                                query = query + this.tb_piso.Text + ",'";
                                query = query + this.tb_dept.Text + "', ";
                                query = query + this.tb_cp.Text + ",'";
                                query = query + this.cb_local.Text + "'";
                                BDConnection.execute(query);

                                for (i = 0; i < (this.dgv_ciudades.Rows.Count - 1); i++)
                                {

                                    query = "EXEC CodingSpree.insertCiudXCli '";
                                    query = query + dgv_ciudades.Rows[i].Cells[0].Value.ToString() + "'";

                                    BDConnection.execute(query);
                                }

                                MessageBox.Show("Alta Efectuada");
                            }

                      }
                      else
                      { //EL USUARIO YA EXISTE DUPLICADO DNI, NOMBRE y APELLIDO


                           MessageBox.Show("Ya existe un usuario con el mismo dni, nombre y apellido.");


                      }

                   }
                   else
                   {

                       MessageBox.Show("Ya existe un usuario registrado con el telefono ingresado.");

                   }

                }
                else
                { 
  
                    MessageBox.Show("Debe completar todos los campos");

                }


            }
            else
            {
                MessageBox.Show("El usuario ya existe."+result);

            }

        }

        private void b_altaP_Click(object sender, EventArgs e)
        {
            string query = "EXEC CodingSpree.existeusuario '" + this.tb_user.Text + "'";
            string result = BDConnection.QueryAndExecuteScalarString(query);


            if (result == "1")
            {
                if (textBox4.Text != "" && textBox5.Text != "" && textBox6.Text != "" && textBox10.Text != "" && textBox12.Text != "" && cb_ciudad.Text != "" && cb_rubro.Text != "" && textBox15.Text != "")
                {

                    if (tb_pass1.Text == tb_pass2.Text)
                    {
                        query = "EXEC CodingSpree.existeProv '" + textBox4.Text + "', '" + textBox12.Text +"'";
                        result = BDConnection.QueryAndExecuteScalarString(query);

                        if (result == "1")
                        { //VERIFICO SI YA EXISTE OTRO PROVEEDOR CON LOS MISMO DATOS.

                            string password = Funciones.ImprimirHash(tb_pass1.Text);
                            int rol = BDConnection.QueryAndExecuteScalar("select id_roles from CodingSpree.TL_ROLES where desc_rol = '" + cb_rol.Text + "'");
                            BDConnection.execute("EXEC CodingSpree.insertUser '" + this.tb_user.Text + "', '" + password + "', " + rol);

                            query = "EXEC CodingSpree.insertProve '";
                            query = query + this.textBox4.Text + "','";

                            string dom = this.textBox10.Text + " " + this.textBox9.Text + " " + this.textBox8.Text + " " + this.textBox7.Text;
                            query = query + dom + "', '";

                            query = query + this.cb_ciudad.Text + "', ";
                            query = query + this.textBox6.Text + ", '";
                            query = query + this.textBox12.Text + "','";
                            query = query + this.cb_rubro.Text + "'";

                            BDConnection.execute(query);


                            MessageBox.Show("Alta Efectuada");
                        }
                        else
                        {

                            MessageBox.Show(" La razon social y el CUIT ya se encuentran registrados.");


                        }
        
                    }

                }
                else
                {
                    MessageBox.Show("Debe completar todos los campos");
                }

            }
            else
            {
                MessageBox.Show("El usuario ya existe.");

            }

          

        }

    }

}    


